﻿using System.Windows;
using System.Windows.Controls;
using Loki.Engine;

namespace Loki.UI.Wpf
{
    public class LokiTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object P_Item, DependencyObject P_Container)
        {
            FrameworkElement L_Element = P_Container as FrameworkElement;
            DataTemplate L_Buffer = null;
            IViewModel L_Model = P_Item as IViewModel;

            if (L_Element != null && L_Model != null)
            {
                L_Buffer = new DataTemplate();
                L_Buffer.DataType = L_Model.GetType();
                Control L_Template = ContextHelper.GetView<IView>(L_Model) as Control;
                L_Buffer.VisualTree = new FrameworkElementFactory(L_Template.GetType());
            }

            if (L_Buffer == null)
            {
                return base.SelectTemplate(P_Item, P_Container);
            }
            else
            {
                return L_Buffer;
            }
        }
    }
}
